package org.app_chat.service.protobuf.integration;

import com.google.protobuf.Message;
import org.app_chat.api.Listener;
import org.app_chat.rabbitmq.ChatRoom;
import org.app_chat.service.protobuf.Test;
import org.junit.Ignore;

import java.util.concurrent.Callable;

import static com.jayway.awaitility.Awaitility.*;

/**
 * Created by Ryan of app-chat.org
 * on: 7/14/12 at 5:47 PM
 */
public class Integration {

    private boolean messageReceived = false;

    /*
   We don't want this test to be run with the build because it depends on the
   activemq broker service running. This is purely an integration test to make
   sure everything is working end to end, and equally a code example to show
   you how you might set up a simple broker using the default broker URL.
    */
    @Ignore
    @org.junit.Test
    public void testCanAddListenerAndReceiveMessage() throws Exception {

        Message protobufMessage = Test
                .SearchRequest
                .newBuilder()
                .setTest("protowin")
                .build();

        ChatRoom chatRoom = ChatRoom
                .builder()
                .listenFor(new TestingListener())
                .build();

        chatRoom.startChat();
        chatRoom.getPublisher().publish(protobufMessage);


//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("localhost");
//
//        Connection receiveConnection = factory.newConnection();
//        Channel receiveChannel = receiveConnection.createChannel();
//        receiveChannel.exchangeDeclare("test", "fanout");
//        String queueName = receiveChannel.queueDeclare().getQueue();
//
//        receiveChannel.queueBind(queueName, "test", "");
//
//        class TestConsumer extends DefaultConsumer {
//
//            public TestConsumer(Channel channel) {
//                super(channel);
//            }
//
//            public void handleDelivery(String consumerTag, Envelope envelope,
//                                       AMQP.BasicProperties properties, byte[] body){
//
//                try {
//                    Thread.sleep(1000);
//
//                    System.out.println(Test.SearchRequest.parseFrom(body) + "oogabooga");
//                    System.out.println(System.currentTimeMillis());
//                    messageReceived = true;
//                } catch (Exception e) {
//                    System.out.println("NO DICE");
//                }
//            }
//        }
//
//        receiveChannel.basicConsume(queueName, true, new TestConsumer(receiveChannel));
//
//
//        ConnectionFactory connectionFactory = new ConnectionFactory();
//        connectionFactory.setHost("localhost");
//
//        Connection publishConnection = connectionFactory.newConnection();
//        Channel publishChannel = publishConnection.createChannel();
//
//
//        publishChannel.exchangeDeclare("test", "fanout");
//
//        publishChannel.basicPublish("test", "", null, protobufMessage.toByteArray());
//        System.out.println(" [x] Sent '" + protobufMessage.toString() + "'" + System.currentTimeMillis());


        await().until(messageIsReceived());


    }

    private Callable<Boolean> messageIsReceived() {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return messageReceived;
            }
        };
    }

    private class TestingListener implements Listener<Test.SearchRequest> {

        @Override
        public void messageReceived(Test.SearchRequest message) {
            System.out.println("Message Received");
            messageReceived = true;
        }

        @Override
        public String getQueueName() {
            return Test.SearchRequest.getDescriptor().getFullName();
        }
    }

}
